<?php

namespace app\api\controller;

use app\services\CommentService;
use app\services\ExchangeService;
use app\services\NoticeService;
use app\services\SubjectService;
use app\services\SystemNoticeService;
use plugin\admin\app\model\Notice;
use support\Request;

class NoticeController
{
    public function unreadCount(Request $request)
    {
        try {
            return ok_rsp('成功', ['notice_count' => NoticeService::unreadCount(), 'system_notice_count' => SystemNoticeService::uneadCount()]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function list(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $type = $request->post('type', Notice::TYPE_OFFICAL_NOTICE);

            switch ($type) {
                case Notice::TYPE_OFFICAL_NOTICE:
                default:
                    $data = NoticeService::list($page, $length, $type);
                    break;
                case Notice::TYPE_SYSTEM_NOTICE:
                    $data = SystemNoticeService::list($page, $length);
                    break;

            }

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function latestMessage(Request $request)
    {
        try {
            [$systemNotice, $notice] = NoticeService::getLatestNotice();

            return ok_rsp('成功', ['system_notice' => $systemNotice, 'notice' => $notice]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function detail(Request $request)
    {
        try {
            $id = $request->post('id');
            $type = $request->post('type', Notice::TYPE_OFFICAL_NOTICE);

            switch ($type) {
                case Notice::TYPE_OFFICAL_NOTICE:
                default:
                    $data = NoticeService::detail($id);
                    break;
                case Notice::TYPE_SYSTEM_NOTICE:
                    $data = SystemNoticeService::detail($id);
                    break;

            }

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function markAllRead(Request $request)
    {
        try {
            $type = $request->post('type', 'all');

            switch ($type) {
                case 'all':
                default:
                    NoticeService::markAllRead();
                    SystemNoticeService::markAllRead();
                    break;
                case 'notice':
                    NoticeService::markAllRead();
                    break;
                case 'system_notice':
                    SystemNoticeService::markAllRead();
                    break;
            }

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function likes(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $data = SubjectService::likes($page, 10);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function comments(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $data = ExchangeService::comments($page, 10);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }
}